<template>
     <main class="basic-info" id="securitySetting">
      <div class="basic-info-content">
        <h1>安全设置</h1>
        <div class="SS-box">
            <div class="security-phone">
                <img src="/static/personal/img/phone.png" alt="">
                <div>
                    <p>手机</p>
                    <p>使用手机绑定：<span>+86 {{phone}}</span></p>
                </div>
            </div>
            <el-form class="form" ref="form" :rules="rules" :model="form" label-width="90px">
              <el-form-item label="手机号码:" prop="phone">
                <el-input class="input-width" v-model="form.phone" maxlength="11" disabled>
                </el-input>
              </el-form-item>
              <!-- <el-form-item label="验证码:" prop="code">
                <div class="flex verification-code">
                  <el-input class="verification-code-inputWidth" v-model="form.code">
                  </el-input>
                  <div class="getCode-btn" @click="getCode">{{isGetCode?'获取验证码': count+ 's后重新获取'}}</div>
                </div>
              </el-form-item> -->
               <el-form-item label="旧密码:" prop="old_pwd">
                <el-input  class="input-width" type="password" placeholder="请输入旧密码" v-model="form.old_pwd">
                </el-input>
              </el-form-item>
              <el-form-item label="新密码:" prop="new_pwd">
                <el-input  class="input-width" type="password" placeholder="设置登录密码，不少于6位数" v-model="form.new_pwd">
                </el-input>
              </el-form-item>
              <el-form-item label="确认密码:" prop="rpassword">
                <el-input class="input-width" type="password" v-model="form.rpassword">
                </el-input>
              </el-form-item>
                <el-form-item>
                  <el-button type="primary" class="submit-btn" @click="submitForm('form')">保存</el-button>
                </el-form-item>
            </el-form>
        </div>
      </div>
    </main>
</template>
<script>
export default {
  name: "securitySetting",
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请确认密码"));
      } else if (value !== this.form.new_pwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      isGetCode: true,
      count: 60,
      phone: '',
      form: {
        phone: "",
        code: "",
        new_pwd: "",
        rpassword: "",
        old_pwd: ''
      },
      rules: {
        phone: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          { len: 11, message: "手机号码为11位数字", trigger: "blur" }
        ],
        // code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
        old_pwd:[{ required: true, message: "请输入旧密码", trigger: "blur" }],
        new_pwd: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, message: "密码不小于6位数", trigger: "blur" }
        ],
        rpassword: [
          { validator: validatePass2, required: true, trigger: "blur" }
        ]
      }
    };
  },
  created() {
    // 获取手机
    this.$http
      .get("/index/user")
      .then(res => {
        // console.warn(res);
          this.form.phone = this.hidePhone(res.data.phone);
          this.phone = this.hidePhone(res.data.phone);
      })
      .catch(err => {});
  },
  methods: {
    // 隐藏手机号
    hidePhone(cellValue) {
        var mobile = String(cellValue);
        var reg = /^(\d{3})\d{4}(\d{4})$/;
        return mobile.replace(reg, "$1****$2");
    },
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // alert("submit!");
          let old_pwd = this.form.old_pwd
          let new_pwd = this.form.new_pwd
          this.$http
            .post("/index/user/changePassword",{
              old_pwd, new_pwd
            })
            .then(res => {
              if(res.data.status == 1){
                this.$message({
                  message: '修改密码成功',
                  type: 'success'
                });
                this.form.old_pwd = ''
                this.form.new_pwd = ''
                this.form.rpassword = ''
              }else{
                this.$message.error(res.data.msg);                
              }
            })
            .catch(err => {});
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    // 获取验证码
    getCode() {
      if (this.isGetCode) {
        this.isGetCode = false;
        this.countTime(60);
      }
    },

    // 倒计时
    // 倒计时
    countTime(e) {
      if (e == 0) {
        this.isGetCode = true;
        return;
      }
      e--;
      this.count = e;
      setTimeout(() => {
        this.countTime(e);
      }, 1000);
    }
  }
};
</script>
<style scoped>
.basic-info {
  box-sizing: border-box;
  position: fixed;
  top: 123px;
  left: 120px;
  width: calc(100vw - 120px);
  height: calc(100vh - 123px);
  padding: 10px 20px;
  background: #e8e8e8;
  padding-bottom: 51px;
  overflow-y: scroll;
}
.basic-info-content {
  background: #fff;
}
.basic-info-content h1 {
  padding: 10px 20px;
  background: #f5f5f7;
  border: 1px solid #ddd;
  color: #333;
  font-weight: 500;
}
.SS-box {
  padding: 20px;
}
.security-phone {
  display: flex;
  align-items: center;
  border: 1px solid #ddd;
  padding: 30px 0 30px 40px;
  background: #f5f5f7;
}
.security-phone img {
  margin-right: 20px;
}
.security-phone div p:first-child {
  margin-bottom: 10px;
  font-size: 16px;
  color: #666;
}
.security-phone div p:last-child {
  color: #888;
}
.security-phone div p:last-child span {
  color: #000;
}
.input-width {
  max-width: 400px;
}
.form {
  margin-left: 30px;
  margin-top: 50px;
}
.flex {
  display: flex;
  align-items: center;
}
.verification-code {
  width: 400px;
}
.verification-code-inputWidth {
  flex: 1;
}
.getCode-btn {
  border: 1px solid #ddd;
  padding: 0 20px;
  margin: 0 0 0 10px;
  font-size: 12px;
  line-height: 26px;
  cursor: pointer;
}
.submit-btn {
  padding: 8px 35px;
  border-radius: 0;
  background: #1590ed;
}
</style>
<style>
#securitySetting .el-input__inner {
  height: 30px !important;
}
#securitySetting .el-input__inner {
  border-radius: 0;
}
</style>
